<?php
/**
 * Created by PhpStorm.
 * User: chenyuz
 * Date: 2019/3/5
 * Time: 12:21
 */
namespace app\ops\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Base extends Controller
{
    protected $userInfo;
    public function __construct(Request $request = null)
    {
        error_reporting(0);
        parent::__construct($request);
        $this->checkLogin();
        $navList = $this->navList($request);
        $this->assign("title", "安吉虎猫");
        $this->assign("navList", $navList);

        $m = $request->module();
        $c = $request->controller();

        $this->assign("now", strtolower("/$m/$c"));
    }

    //检查是否登录
    public function checkLogin() {
        $this->userInfo = Session::get("userInfo");
        if (empty($this->userInfo)) {
            $this->error("请先登录", "/ops/Login/index");
        }
    }

    public function navList($request) {
        $url = $request->url();
        $modularr = explode('/', $url);
        $modul = explode('?', $modularr[2]);
        $navModulArr = ['', 'Index', 'User', 'Jobs', 'Company'];
        $parentId = 0;
        if (in_array($modul[0], $navModulArr)) {
            $keys = array_keys($navModulArr, $modul[0]);
            $parentId = $keys[0];
        } else {
            //$this->error("非法访问");
        }

        $navOnelist = Db::name('admin_nav')
            ->field(['nav_id', 'nav_name', 'url', 'type', 'path'])
            ->where(["status"=> 1, "type"=> 1, "parent_id"=> 0])
            ->select();

        $navOnelist2 = Db::name('admin_nav')
            ->field(['nav_id', 'nav_name', 'url', 'type', 'path'])
            ->where(["status"=> 1, "type"=> 2, "parent_id"=> 0])
            ->select();


//        dump($navOnelist2);
        return [$navOnelist, $navOnelist2, $parentId];
    }
}